﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyDMS_ExtJS.Admin
{
    public partial class Backup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //在DropDownList中绑定所有数据库
                SqlConnection connection = new SqlConnection(
                    ConfigurationManager.ConnectionStrings["MyDMSConStr"].ConnectionString);
                SqlCommand command = new SqlCommand("sp_helpdb", connection);
                command.CommandType = CommandType.StoredProcedure;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                ddlDatabaseList.DataSource = reader;
                ddlDatabaseList.DataTextField = "Name";
                ddlDatabaseList.DataValueField = "Name";
                ddlDatabaseList.DataBind();
                reader.Close();
                connection.Close();
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string dbFileName = txtDbFileName.Text.Trim();
            SqlConnection connection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["MyDMSConStr"].ConnectionString);
            //string dbName = ddlDatabaseList.SelectedValue;
            string dbName = "mydms";
            if (!dbFileName.EndsWith(".bak"))
            {
                dbFileName += ".bak";
            }
            if (rbBackup.Checked) //备份数据库
            {
                SqlCommand command = new SqlCommand("use master;backup database @name to disk=@path;", connection);
                connection.Open();
                string path = Server.MapPath("~//App_Data") + "//" + dbFileName;
                command.Parameters.AddWithValue("@name", dbName);
                command.Parameters.AddWithValue("@path", path);
                command.ExecuteNonQuery();
                connection.Close();
            }
            else //恢复数据库
            {
                SqlCommand command = new SqlCommand("use master;restore database @name from disk=@path;", connection);
                connection.Open();
                string path = Server.MapPath("~//App_Data") + "//" + dbFileName;
                command.Parameters.AddWithValue("@name", dbName);
                command.Parameters.AddWithValue("@path", path);
                command.ExecuteNonQuery();
                connection.Close();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string dbFileName = txtFileName.Text.Trim();
            if (!dbFileName.EndsWith(".bak"))
            {
                dbFileName += ".zip";
            }
            if (rbPack.Checked) //备份数据库
            {
                SharpZip.PackFiles(Server.MapPath("~//App_Data") + "//" + dbFileName, Server.MapPath("~//Upload") + "//");
            }
            else //恢复数据库
            {
                SharpZip.UnpackFiles(Server.MapPath("~//App_Data") + "//" + dbFileName, Server.MapPath("~//Upload") + "//");
            }
        }
    }
}